<?php
namespace app\car\api;
use think\Controller;
use think\Request;

class Pool extends Controller
{
    /**
     * 拼车首页
     * http://api.ybcpq.cn/car/pool/index
     */
    public function index()
    {
        $data = array();
        $data['banner'] = model("Banner")->getBanner();
        $data['announcement'] = model("Cms")->getAnnouncement();
        $data['category'] = model("Category")->getCategory();

        $result = array('code'=>SUCCESS,'msg'=>'Success','data'=>$data);
        return json($result);
    }
    /**
     * 拼车首页分类筛选
     * http://api.ybcpq.cn/car/pool/getListByCategory
     */
    public function getListByCategory()
    {
        $pid = Request::instance()->param('category',0,'int');
        $pageno   = Request::instance()->param('pageno',1,'int');
        $pagesize = Request::instance()->param('pagesize',10,'int');

        $model = model("Pool");
        $data = $model->getListByCategory($pid,$pageno,$pagesize);

        $result = array('code'=>SUCCESS,'msg'=>'Success','data'=>$data);
        return json($result);
    }
    /**
     * 拼车信息添加
     * http://api.ybcpq.cn/car/pool/poolAdd
     */
    public function poolAdd()
    {
        $data = input("post.");

        $validate = validate("Pool");
        if(!$validate->check($data)){
            $result = array('code'=>VALIDATE_ERROR,'msg'=>'param error','data'=>'');
            return json($result);
        }

        $model = model("Pool");
        $res = $model->poolAdd($data);

        if($res !== false){
            $result = array('code'=>SUCCESS,'msg'=>'Success','data'=>'');
        }else{
            $result = array('code'=>ERROR,'msg'=>'Error','data'=>'');
        }
        return json($result);
    }
    /**
     * 拼车信息更改
     * http://api.ybcpq.cn/car/pool/poolEdit
     */
    public function poolEdit()
    {
        $data = input("post.");

        $validate = validate("Pool");
        if(!$validate->check($data)){
            $result = array('code'=>VALIDATE_ERROR,'msg'=>'param error','data'=>'');
            return json($result);
        }

        $model = model("Pool");
        $res = $model->poolEdit($data);

        if($res !== false){
            $result = array('code'=>SUCCESS,'msg'=>'Success','data'=>'');
        }else{
            $result = array('code'=>ERROR,'msg'=>'Error','data'=>'');
        }
        return json($result);
    }
    /**
     * 拼车详情
     * http://api.ybcpq.cn/car/pool/poolDetail
     */
    public function poolDetail()
    {
        $id = Request::instance()->param('id',0,'int');

        $model = model("Pool");
        $res = $model->poolDetail($id);

        $result = array('code'=>SUCCESS,'msg'=>'Success','data'=>$res);
        return json($result);
    }

    /**
     * 我的拼车
     * http://api.ybcpq.cn/car/pool/myPoolList
     */
    public function myPoolList()
    {
        $pageno   = Request::instance()->param('pageno',1,'int');
        $pagesize = Request::instance()->param('pagesize',5,'int');

        $model = model("Pool");
        $data = $model->myPoolList($pageno,$pagesize);

        $result = array('code'=>SUCCESS,'msg'=>'Success','data'=>$data);
        return json($result);

    }

    /**
     * 撤销行程
     * http://api.ybcpq.cn/car/pool/myPoolDel
     */
    public function myPoolDel()
    {
        $id = Request::instance()->param('id',0,'int');

        $model = model("Pool");
        $res = $model->myPooldel($id);

        if($res !== false){
            $result = array('code'=>SUCCESS,'msg'=>'Success','data'=>'');
        }else{
            $result = array('code'=>ERROR,'msg'=>'Error','data'=>'');
        }
        return json($result);
    }
}